import login from "../views/login.ejs";
import {postLogin} from "../api/adminListApi";
const loginHandler = function(router){
	return async function (e) {
		e.preventDefault();// 移除默认行为。
		const form = document.loginForm;
		const adminName = form.adminName.value.trim();
		const passWord = form.passWord.value.trim();
		if (adminName.length === 0) {
			toastr.error("请输入管理员账号");
			return;
		}
		if (passWord.length === 0) {
			toastr.error("请输入管理员密码");
			return;
		}
		const {token} = await postLogin({adminName,passWord});
		// 保存管理员账号至Storage中
		localStorage.setItem("adminName",adminName);
		localStorage.setItem("token",token);
		toastr.success("登陆成功")
		// 跳转至管理员列表界面
		router.go("/index/adminList");// sme-router
	}
}
export default function (router) {
	return (req, res) => {
		res.render(login());
		// 为ID为loginBtn的按钮增加单事件
		document.querySelector("#loginBtn").onclick = loginHandler(router);
	}
}